package com.whlilong.jxc.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.hibernate.CacheMode;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;

import com.whlilong.jxc.constants.IAppConstants;
import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.db.HibernateSessionFactory;
import com.whlilong.jxc.model.Users;
import com.whlilong.jxc.sys.CacheImage;
import com.whlilong.jxc.sys.IImageKey;
import com.whlilong.jxc.sys.SaveLogInfo;
@SuppressWarnings("unchecked")
public class LoginDialog extends Dialog {

	private Combo usernameCombo;
	private Text passwordText;
	
	private List<Users> userVector = new ArrayList<Users>();;

	public LoginDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}
	
	protected Control createDialogArea(Composite parent) {
		// create a composite with standard margins and spacing
		Composite container = (Composite) super.createDialogArea(parent);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		gridLayout.marginRight = 6;
		gridLayout.makeColumnsEqualWidth = false;
		container.setLayout(gridLayout);
		GridData gd = new GridData(GridData.FILL_BOTH);
		container.setLayoutData(gd);

		final CLabel title = new CLabel(container, SWT.CENTER);
		title.setFont(new Font(Display.getCurrent(), "隶书", 12, SWT.BOLD
				| SWT.ITALIC));
		title.setText("欢迎使用立隆商务信息管理系统");
		title.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.title));
		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 3;
		title.setLayoutData(gd);

		final CLabel label = new CLabel(container, SWT.RIGHT);
		label.setText("用户名");
		// label.setImage(CacheImage.getINSTANCE().getImage(
		// IAppConstants.APPLICATION_ID, IImageKey.LOGOFF));
		gd = new GridData(GridData.FILL_BOTH);
		gd.widthHint = 100;
		label.setLayoutData(gd);
		usernameCombo = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.widthHint = 140;
		usernameCombo.setLayoutData(gd);
		userVector = getUserName();
		Iterator<Users> iter = userVector.iterator();
		int i = 0;
		while (iter.hasNext()) {
			if (i % 2 == 0){
				Users  users = iter.next();
				usernameCombo.add(users.getUsername());
				usernameCombo.setData(users.getUsername(),users);
			}
			i++;
		}
		usernameCombo.select(0);

		Label empty = new Label(container, SWT.NONE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.widthHint = 60;
		empty.setLayoutData(gd);

		final CLabel label2 = new CLabel(container, SWT.RIGHT);
		label2.setText("密　码");
		// label2.setImage(CacheImage.getINSTANCE().getImage(
		// IAppConstants.APPLICATION_ID, IImageKey.PASSWORD));
		gd = new GridData(GridData.FILL_BOTH);
		gd.widthHint = 100;
		label2.setLayoutData(gd);
		passwordText = new Text(container, SWT.BORDER);
		passwordText.setEchoChar('*');
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.widthHint = 140;
		passwordText.setLayoutData(gd);
		passwordText.setFocus();
		Label empty1 = new Label(container, SWT.NONE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.widthHint = 60;
		empty1.setLayoutData(gd);

		return container;
	}

	private List getUserName() {
		//List<String> vector = new ArrayList<String>();
		/*Session session = HibernateSessionFactory
				.getSession("hibernate_derby.cfg.xml");
		Query query = session
				.getNamedQuery("com.whlilong.jxc.ui.LoginDialog.getUserName");*/
		//ScrollableResults result = query.setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
		List result = (List)DbManager.getInstance().search("com.whlilong.jxc.ui.LoginDialog.getUserName");
		return result;
	}

	protected void createButtonsForButtonBar(final Composite parent) {
		// create OK and Cancel buttons by default
		createButton(parent, IDialogConstants.OK_ID, "登录", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "退出", false);
	}

	protected void okPressed() {
		String username = usernameCombo.getText();
		String password = passwordText.getText();
		MessageBox mb;
		if (username == null || username.trim().length() == 0) {
			mb = new MessageBox(getShell(), SWT.ICON_INFORMATION | SWT.OK);
			mb.setText("提示");
			mb.setMessage("用户名不能为空!");
			mb.open();
			usernameCombo.setFocus();
			return;
		}
		/*
		 * if (password == null || password.trim().length() == 0) { mb = new
		 * MessageBox(getShell(), SWT.ICON_INFORMATION | SWT.OK);
		 * mb.setText("提示"); mb.setMessage("密码不能为空!"); mb.open();
		 * passwordText.setFocus(); return; }
		 */
		SaveLogInfo.getInstance().setUsername(username);
		SaveLogInfo.getInstance().setPassword(password);
		super.okPressed();
	}

	protected Point getInitialSize() {
		return new Point(350, 200);
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("操作员登录");
		// newShell.setImage(CacheImage.getINSTANCE().getImage(
		// IAppConstants.APPLICATION_ID, IImageKey.WINDOW_IMAGE));
	}
	
}
